package com.example.jetpackpaging.viewmodel

import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import androidx.paging.Pager
import androidx.paging.PagingConfig
import androidx.paging.PagingData
import androidx.paging.cachedIn
import com.example.jetpackpaging.model.Repo
import com.example.jetpackpaging.paging.RepoPagingSource
import kotlinx.coroutines.flow.Flow

/**
 * 按照规范 ViewModel不可以直接操作PagingSource，ViewModel操作Repository,Repository再去操作PagingSource
 */
class RepoViewModel: ViewModel() {
    //repos当作属性保存值，放置屏幕旋转值丢失
    private val repos by lazy {
        Pager(config = PagingConfig(pageSize = 8, initialLoadSize = 16),
            pagingSourceFactory = {RepoPagingSource()}).flow.cachedIn(viewModelScope)
    }

    fun loadRepoData(): Flow<PagingData<Repo>> {
        return repos
    }

}